package com.party.mem.biz.system.menu;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.party.common.utils.StringUtils;
import com.party.core.model.member.MemberGroup;
import com.party.core.model.menu.Menu;
import com.party.core.service.member.IMemberGroupService;
import com.party.core.service.menu.IMenuService;
import com.party.mem.utils.RealmUtils;
import com.party.mem.web.security.CurrentUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


/**
 * 菜单
 * Created by Juliana
 */

@Service
public class MenuBizService {

    @Autowired
    private IMenuService menuService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private IMemberGroupService memberGroupService;

    private static final  String WEB_MAIN_MENU_ID = "2";


    public List<Map> buildWebData(boolean isSetRedis){
        CurrentUser currentUser = RealmUtils.getNewCurrentUser();
        String str = (String) redisTemplate.opsForValue().get("myMemberMenuTree_"+currentUser.getId());
        if (!(StringUtils.isEmpty(str) || "[]".equals(str))) {
            return JSONArray.parseArray(str, Map.class);
        } else {
            List<Menu> menus = menuService.listAll();


            //只保留会员管理-会员列表菜单
            removeOtherMenuList(menus);


            // 绑定商户信息后才能有众筹菜单
            removeZcMenuList(menus, currentUser);

            // 倒序排列
            Collections.reverse(menus);

            // 权限代码集合
            List<String> permissions = menuService.buildPermission(currentUser.getId(), null);
            List<Map<String, Object>> maps = menuService.buildTreeData(WEB_MAIN_MENU_ID, menus, permissions, currentUser.isChild());
            String s = JSONArray.toJSONString(maps);
            if (isSetRedis) {
                redisTemplate.opsForValue().set("myMemberMenuTree_" + currentUser.getId(), s);
            }
            return JSONArray.parseArray(s, Map.class);
        }
    }

    /**
     * 移出其他菜单，只保留会员管理-会员列表菜单
     * @author shenliuhai
     * @date 2019/12/23 16:51
     * @param menus
     * @return void
     */
    private void removeOtherMenuList(List<Menu> menus) {
        Iterator<Menu> iterator = menus.iterator();

        while (iterator.hasNext()) {
            Menu m = iterator.next();
            String name = m.getName();
            if ("会员管理".equals(name) || "会员列表".equals(name) ||
                "管理功能".equals(name) || "web菜单".equals(name) || "自定义信息收集".equals(name)) {
                continue;
            }
            iterator.remove();
        }
    }
    /**
     * 绑定商户信息后才能有众筹菜单
     * @param menus
     * @param member
     */
    private void removeZcMenuList(List<Menu> menus, MemberGroup member) {
        member = memberGroupService.getPartner(member.getId());
        if (!new Integer(1).equals(member.getIsPartner())) {
            return;
        }
        if (new Integer(1).equals(member.getOpenStatus())) {
            return;
        }
        Iterator<Menu> iterator = menus.iterator();
        String id = null;
        while (iterator.hasNext()) {
            Menu m = iterator.next();
            String name = m.getName();
            id = m.getId();
            if ("众筹管理".equals(name)) {
                iterator.remove();
                break;
            }
        }

        Iterator<Menu> iterator2 = menus.iterator();
        while (iterator2.hasNext()) {
            Menu m = iterator2.next();
            String parentId = m.getParentId();
            if (id.equals(parentId)) {
                iterator2.remove();
            }
        }
    }

    public String getFirstMenuUrl() {
        List<Map> maps = this.buildWebData(false);
        if (maps.size() > 0) {
            Map map = maps.get(0);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("href", map.get("href"));
            jsonObject.put("children", map.get("children"));
            return getFirstHref(jsonObject);
        }
        return "";
    }

    public String getFirstHref(JSONObject root) {
        JSONArray children = root.getJSONArray("children");
        if (children.size() > 0) {
            JSONObject root1 = children.getJSONObject(0);
            return getFirstHref(root1);
        } else {
            return root.get("href").toString();
        }
    }
}
